package idu0080.veebitoorik.web;

import idu0080.veebitoorik.serviceclient.SOAPClient;
import idu0080.yl6_OpenERPServices_client.Invoice;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PaymentServlet
 */
@WebServlet("/payment")
public class PaymentServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaymentServlet() 
    {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String selectedInvoiceNumber = null;
		Invoice selectedInvoice = null;
		SOAPClient soapClient = new SOAPClient();
		String paymentNumber = "";
		Double paydAmount = 0.0;
		String UNSUCCESSFUL_PAYMENT_INDICATOR = "-1";
		
		if ((request.getParameter("selectedInvoiceNumber") != null) && (!request.getParameter("selectedInvoiceNumber").equals("")))
		{
			selectedInvoiceNumber = request.getParameter("selectedInvoiceNumber");
			selectedInvoice = soapClient.getInvoiceByNumber(selectedInvoiceNumber);
			request.setAttribute("selectedInvoice", selectedInvoice);
			request.setAttribute("selectedInvoiceNumber", selectedInvoiceNumber);
		}
		
		if ((request.getParameter("paydAmount") != null) && (!request.getParameter("paydAmount").equals("")))
		{
			paydAmount = Double.valueOf(Double.parseDouble(request.getParameter("paydAmount")));
		}
		
		if ((request.getParameter("what_to_do").equals("makePayment")) && (paydAmount.doubleValue() > 0.0D) && (selectedInvoice != null))
		{
			paymentNumber = soapClient.makePayment(selectedInvoice.getNumber(), selectedInvoice.getPartnerId(), paydAmount);
			
			if (!paymentNumber.equals(UNSUCCESSFUL_PAYMENT_INDICATOR))
			{
				request.setAttribute("paymentNumber", paymentNumber);
				selectedInvoice = soapClient.getInvoiceByNumber(selectedInvoiceNumber);
				request.setAttribute("selectedInvoice", selectedInvoice);
			}
			else
			{
				System.out.println("SOAP ERROR CODE:" + soapClient.getErrorCode());
				System.out.println("SOAP ERROR MESSAGE:" + soapClient.getErrorMessage());
				request.setAttribute("SOAP_ERROR_CODE", soapClient.getErrorCode());
				request.setAttribute("SOAP_ERROR_MESSAGE", soapClient.getErrorMessage());
			}
		}
		
		getServletConfig().getServletContext().getRequestDispatcher("/payment.jsp").forward(request, response);
	}

}
